package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 存款界面
 */
public class SavePanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private JLabel infoLabel = new JLabel();
    private JTextField blanceTextField = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("images/buttonSure.jpg"));
    private Font font = new Font("黑体",Font.BOLD,30);
    public SavePanel(ATMFrame atmFrame){
        this.atmFrame = atmFrame;
        this.setLayout(null);
        infoLabel.setBounds(300,200,300,50);
        infoLabel.setFont(font);
        infoLabel.setText("请输入存款的金额：");

        blanceTextField.setBounds(600,200,200,50);

        sureBtn.setBounds(350,400,140,50);
        sureBtn.addActionListener(this);

        this.add(infoLabel);
        this.add(blanceTextField);
        this.add(sureBtn);
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            User user = atmFrame.getLoginUser();
            try{
                user.setBlance(user.getBlance() + Double.parseDouble(blanceTextField.getText()));
                // 修改数据库的数据
                UserDao userDao = new UserDao();
                userDao.update(user);
//                FileUtil.writeObject("users.dat",atmFrame.getUsers());
            }catch (NumberFormatException e1){
                JOptionPane.showMessageDialog(this,"必须输入数字");
                return;
            }
            atmFrame.switchPanel("menu");
        }
    }
}
